package com.lbj.service.user;

import com.lbj.bean.User;
import com.lbj.dao.BaseDao;
import com.lbj.dao.user.UserDao;
import com.lbj.dao.user.UserDaoImpl;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }

    @Override
    public User login(String userCode, String password) {
        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getUser(connection, userCode);
            // 判断用户为空或者用户密码不一致返回null
            if (user == null || !password.equals(user.getUserPassword())) {
                return null;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.release(connection, null, null);
        }
        return user;
    }

    @Override
    public boolean updatePwd(String userId, String password) {
        System.out.println("UserService:" + password);

        Connection connection = null;
        connection = BaseDao.getConnection();
        boolean flag = false;
        try {
            int result = userDao.updatePwd(connection, userId, password);
            flag = result > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(connection, null, null);
        }
        return flag;
    }

    @Override
    public int getUserCount(String username, int userRole) {
        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            count = userDao.getUserCount(connection, username, userRole);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(connection, null, null);
        }
        return count;
    }

    @Override
    public List<User> getUserList(String username, int userRole, int index, int pageSize) {
        Connection connection = null;
        List<User> list = null;
        try {
            connection = BaseDao.getConnection();
            list = userDao.getUserList(connection, username, userRole, index, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(connection, null, null);
        }
        return list;
    }

    @Test
    public void test() {
        UserServiceImpl impl = new UserServiceImpl();
        User admin = impl.login("test", "123");
        if (admin == null) {
            System.out.println("没有找到用户");
        } else {
            System.out.println("用户名：" + admin.getUserName());
        }
    }

    @Test
    public void testUserCount() {
        UserServiceImpl impl = new UserServiceImpl();
        int userCount = impl.getUserCount("张", 3);
        System.out.println("查询的结果为：" + userCount);
    }

    @Test
    public void testUserList() {
        UserServiceImpl impl = new UserServiceImpl();
        List<User> userList = impl.getUserList("张", 3, 0, 5);
        for (User user : userList) {
            System.out.println("查询的结果为：" + user.getUserName());
        }
    }
}
